package com.songlanyun.common.drive;

import com.songlanyun.common.validator.ValidatorUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class ValidatorAspect {

    @Pointcut("@annotation(com.songlanyun.common.drive.Validator)")
    public void validateCut()
    {

    }

    @Before("validateCut()")
    public void beforeProcess(JoinPoint point)
    {
        Object params = point.getArgs()[0];
        MethodSignature sign = (MethodSignature) point.getSignature();
        Method method = sign.getMethod();
        //获取方法上的注解
        Validator annotation = method.getAnnotation(Validator.class);
        if(annotation==null) return ;
        if(params instanceof RootEntity)
        {

            ValidatorUtils.validateEntity(params,annotation.value());
        }
    }
}
